package com.gxjtkyy.datadistributer.writer.rdbms.service;

import com.gxjtkyy.datadistributer.writer.rdbms.config.DbRecordFilterConfig;
import com.gxjtkyy.datadistributer.writer.rdbms.constant.DbRecordFilterType;
import com.gxjtkyy.datadistributer.writer.rdbms.vo.DbTableSetting;
import java.util.Iterator;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

@Service
public class DbRecordFilterService {
   private final DbWriterConfigService dbWriterConfigService;
   private final QueryService queryService;

   public boolean isIgnoredByColumn(String tableName, String columnName, String columnValue) {
      DbTableSetting dbTableSetting = this.dbWriterConfigService.getDbTableSetting(tableName);
      Iterator var5 = dbTableSetting.getDbRecordFilterConfigs().iterator();

      while(var5.hasNext()) {
         DbRecordFilterConfig dbRecordFilterConfig = (DbRecordFilterConfig)var5.next();
         String filterColumn = dbRecordFilterConfig.getFilterColumn();
         if (filterColumn.equalsIgnoreCase(columnName)) {
            DbRecordFilterType filterType = dbRecordFilterConfig.getFilterType();
            Stream var10000;
            QueryService var10001;
            Optional reduce;
            if (filterType == DbRecordFilterType.STATION_ID) {
               var10000 = dbRecordFilterConfig.getOrgaCodes().stream();
//               var10001 = this.queryService;
//               Objects.requireNonNull(var10001);
               reduce = dbRecordFilterConfig.getOrgaCodes().stream().map(queryService::getRelativeStationIds).reduce((ids1, ids2) -> {
                  ids1.addAll(ids2);
                  return ids1;
               });
            } else {
               if (filterType != DbRecordFilterType.GANTRY_ID) {
                  throw new RuntimeException(String.format("未指定过滤类型,table=%s,column=%s", tableName, columnName));
               }

               var10000 = dbRecordFilterConfig.getOrgaCodes().stream();
               var10001 = this.queryService;
               Objects.requireNonNull(var10001);
               reduce = dbRecordFilterConfig.getOrgaCodes().stream().map(queryService::getRelativeGantryIds).reduce((ids1, ids2) -> {
                  ids1.addAll(ids2);
                  return ids1;
               });
            }

            if (!reduce.isPresent()) {
               return true;
            }

            if (Strings.isBlank(columnValue)) {
               return true;
            }

            if (!((Set)reduce.get()).contains(columnValue.trim())) {
               return true;
            }
         }
      }

      return false;
   }

   public DbRecordFilterService(final DbWriterConfigService dbWriterConfigService, final QueryService queryService) {
      this.dbWriterConfigService = dbWriterConfigService;
      this.queryService = queryService;
   }
}
